/* 
 * Project: FlarDetector
 *
 * Copyright 2009, Longfellow Chen
 * --------------------------------------------------------------------------------
 * This work complements FLARToolkit, developed by Saqoosha as part of the Libspark project.
 *	http://www.libspark.org/wiki/saqoosha/FLARToolKit
 * FLARToolkit is Copyright (C)2008 Saqoosha,
 * and is ported from NYARToolkit, which is ported from ARToolkit.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * For further information please contact:
 *	<fellong0307@gmail.com>
 *	http://fellong.blogspot.com/
 * 
 */
 
 package com.fellong.flash.flar.object.ext
{
	import com.fellong.flash.flar.detector.FlarDetector;
	import com.fellong.flash.flar.object.FlarObject;
	
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.utils.BitmapMaterialTools;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Cube;
	import org.papervision3d.objects.primitives.Plane;

	public class FlarHole extends FlarObject
	{		
		[Embed(source='Data/cube-side.png')]
		private static const cubeSideTextureClass:Class;
		[Embed(source='Data/cube-bottom.png')]
		private static const cubeBottomTextureClass:Class;
		
		public static const DOOR_NONE:int = 0;
		public static const DOOR_PLANE:int = 1;
		public static const DOOR_CUBE:int = 2;
		
		private var hole:Cube = null;
		private var green:Cube = null;
		
		private var _has_door:Boolean;
		private var _timer:Timer = null;
		private var _door:DisplayObject3D = null;
		private var _door_type:int = 0;

		public function FlarHole( door_type:int = DOOR_CUBE )
		{
			super();				
			_door_type = door_type;
			this._detector.addEventListener( FlarDetector.REGIST, onREGIST );
		}
		
		private function onREGIST( event:Event ):void
		{
			this.pivot.removeChild(hole);
			this.pivot.removeChild(green);
			var cSize:uint = this._detector.codeWidth;
			var cmats:BitmapMaterial = BitmapMaterialTools.createBitmapMaterial(cubeSideTextureClass);
			var cmatb:BitmapMaterial = BitmapMaterialTools.createBitmapMaterial(cubeBottomTextureClass);
			var hole:Cube = new Cube(new MaterialsList({all: cmats, bottom: cmatb}), cSize, cSize, cSize, 1, 1, 1, Cube.ALL, Cube.TOP);
			var green:Cube = new Cube(new MaterialsList({all: new ColorMaterial(0x00ff00)}), cSize, cSize, cSize, 1, 1, 1, 0, Cube.TOP);
			hole.y = green.y = -cSize/2;
			this.pivot.addChild(hole);
			this.pivot.addChild(green);
			
			if( _door_type != DOOR_NONE )
			{
				var _bodyMaterial:BitmapMaterial;
				_bodyMaterial = new BitmapMaterial( this._detector.toBitmap() );
				
				this.pivot.removeChild(_door);
				_door = new DisplayObject3D();
				
				var _plane_body:DisplayObject3D;
				switch( _door_type )
				{
					case DOOR_CUBE:
						var ml:MaterialsList = new MaterialsList({all: cmats, front: cmatb, back:_bodyMaterial});
						_plane_body = new Cube( ml,cSize,cSize/24,cSize,1,1,1);
						break;
				
					case DOOR_PLANE:
						_bodyMaterial.doubleSided = true;
						_plane_body = new Plane( _bodyMaterial,cSize,cSize,1,1);
						break;
				}
				_plane_body.rotationX = 90;
				_plane_body.z=-cSize/2;
				_door.addChild(_plane_body);
				_door.z = cSize/2;
				_door.rotationX = 0;
				this.pivot.addChild(_door);
				
				_timer = new Timer( 33 );
				_timer.addEventListener( TimerEvent.TIMER, onTIMER_COMPLETE );
				_timer.start();
			}
		}
		
		private function onTIMER_COMPLETE( event:TimerEvent ):void 
		{
			if( _door.rotationX < 100 )
				_door.rotationX += 3;
			else
				_timer.stop();
		}

		override protected function onMARKER_ADDED( event:Event ):void
		{			
			super.onMARKER_ADDED(event);
			if( _door_type != DOOR_NONE )
			{
				_door.rotationX = 0;
				_timer.reset();
				_timer.start();
			}
		}

	}
}